package com.cloudsigns.factorymanager2.produce.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

public class ProduceEditOrderWorkpartAdapter extends BaseAdapter {
    private String TAG = ProduceEditOrderWorkpartAdapter.class.getSimpleName();
    private Context context;
    private List<WorkPartBean> datas = new ArrayList<WorkPartBean>();
    private LayoutInflater inflater;
    private ItemRemoveListener itemRemoveListener;
    private ItemClickListener itemClickListener;

    public ProduceEditOrderWorkpartAdapter(Context context, String TAG) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = TAG;
    }

    public void setData(List<WorkPartBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }
    }

    public void setRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public WorkPartBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_produce_edit_order_workpart, null);
            holder.tv_process_name = convertView.findViewById(R.id.tv_process_name);
            holder.tv_last = convertView.findViewById(R.id.tv_last);
            holder.img_delete = convertView.findViewById(R.id.img_delete);
            holder.img_date = convertView.findViewById(R.id.img_date);
            holder.img_edit = convertView.findViewById(R.id.img_edit);
            holder.rl_process_information = convertView.findViewById(R.id.rl_process_information);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final WorkPartBean workPartBean = datas.get(position);
        holder.tv_process_name.setText(workPartBean.getWorkPartName());
        if (workPartBean.isLastWorkPart()) {
            holder.tv_last.setVisibility(View.VISIBLE);
        } else {
            holder.tv_last.setVisibility(View.GONE);
        }
        if (workPartBean.isEdit()) {
            holder.rl_process_information.setVisibility(View.VISIBLE);
        } else {
            holder.rl_process_information.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(workPartBean.getWorkPartId())){
            holder.img_delete.setVisibility(View.GONE);
        }else{
            holder.img_delete.setVisibility(View.VISIBLE);
        }


        // 删除
        holder.img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemRemoveListener != null) {
                    if (workPartBean.isLastWorkPart()) {
                        Toast.makeText(context, "【" + workPartBean.getWorkPartName() + "】为最后一道工序，不可以删除！", Toast.LENGTH_LONG).show();
                    } else {
                        itemRemoveListener.remove(position);
                    }
                }
            }
        });

        // 计划
        holder.img_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.itemClick(view, position);
                }
            }
        });

        // 编辑
        holder.img_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.itemClick(view, position);
                }
            }
        });

        // 加工信息
        holder.rl_process_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.itemClick(view, position);
                }
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView tv_process_name;
        TextView tv_last;
        ImageView img_delete;
        ImageView img_date;
        ImageView img_edit;
        RelativeLayout rl_process_information;
    }
}
